package net.xdclass.base_project.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.concurrent.Future;

/**
 * @author sqz
 * @Description: 异步任务业务类
 * @date 2019/3/23 14:34
 */
@Component
@Async
public class AsyncTask {

    public void task1() throws InterruptedException {
        Instant begin = Instant.now();
        Thread.sleep(1000);
        Instant end = Instant.now();
        Duration duration = Duration.between(begin,end);
        System.out.println("任务1耗时："+duration.toMillis());
    }
    public void task2() throws InterruptedException {
        Instant begin = Instant.now();
        Thread.sleep(3000);
        Instant end = Instant.now();
        Duration duration = Duration.between(begin,end);
        System.out.println("任务2耗时："+duration.toMillis());
    }
    public void task3() throws InterruptedException {
        Instant begin = Instant.now();
        Thread.sleep(2000);
        Instant end = Instant.now();
        Duration duration = Duration.between(begin,end);
        System.out.println("任务3耗时："+duration.toMillis());
    }

    //获取异步结果

    public Future<String> task4() throws InterruptedException {
        Instant begin = Instant.now();
        Thread.sleep(3000);
        Instant end = Instant.now();
        Duration duration = Duration.between(begin,end);
        System.out.println("任务4耗时："+duration.toMillis());
        return new AsyncResult<>("任务4");

    }
    public Future<String> task5() throws InterruptedException {
        Instant begin = Instant.now();
        Thread.sleep(1000);
        Instant end = Instant.now();
        Duration duration = Duration.between(begin,end);
        System.out.println("任务5耗时："+duration.toMillis());
        return new AsyncResult<>("任务5");
    }
    public Future<String> task6() throws InterruptedException {
        Instant begin = Instant.now();
        Thread.sleep(2000);
        Instant end = Instant.now();
        Duration duration = Duration.between(begin,end);
        System.out.println("任务6耗时："+duration.toMillis());
        return new AsyncResult<>("任务6");
    }
}
